package com.boomkeeper.core.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.json.JacksonJsonParser;

public class JsonUtils {
	
	private static final Logger LOGGER = LoggerFactory.getLogger(JsonUtils.class);
	
	public static Map<String, Object> parsejsonStr2Map(String jsonStr){
		return new JacksonJsonParser().parseMap(jsonStr);
	}
	
	public static String parseJsonFile2String(File jsonFile) {
		InputStream is = null;
		try {
			is = new FileInputStream(jsonFile);
			byte[] jsonBytes = new byte[is.available()];
			is.read(jsonBytes);
			return new String(jsonBytes);
		} catch (Exception e) {
			LOGGER.error("解析json文件为字符串异常", e);
			System.exit(0);
		} finally {
			if (is != null) {
				try {
					is.close();
				} catch (Exception e) {
					LOGGER.error("解析json文件为字符串异s常", e);
					System.exit(0);
				}
			}
		}
		return null;
	}
	
//	public static JSONObject parse2jsonStrObject(String jsonStr) {
//		return new JSONObject(jsonStr);
//	}
	
	public static Map<String, Object> parseJsonFile2Map(File jsonFile) {
		String jsonStr = JsonUtils.parseJsonFile2String(jsonFile);
//		LOGGER.debug("json文件解析为字符串：{}", jsonStr);
		return JsonUtils.parsejsonStr2Map(jsonStr);
	}
}
